package com.imageim.yimirror.ui.widght;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

/**
 * Created by Yuaihen on 2018/3/7.
 */

public class RoundCornerImageView extends android.support.v7.widget.AppCompatImageView {

    //圆角弧度
    private float[] rids = {8.0f, 8.0f, 8.0f, 8.0f, 8.0f, 8.0f, 8.0f, 8.0f};
    private Path mPath;

    public RoundCornerImageView(Context context) {
        super(context);
        init();
    }

    public RoundCornerImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RoundCornerImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPath = new Path();
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();
        //绘制圆角
        mPath.addRoundRect(new RectF(0, 0, width, height), rids, Path.Direction.CW);
        canvas.clipPath(mPath);
        super.onDraw(canvas);

    }
}
